﻿@using DeliveryPlus.Web.DAL
@model System.Collections.Generic.List<DeliveryPlus.Web.DAL.Entities.Dishe>

@{
    ViewBag.Title = "Список всех блюд";
    Layout = "../Shared/ControlPanelLayout.cshtml";
    var dc = new DeliveryPlusDataContext();
    var dishTypes = dc.DishTypes.OrderBy(t => t.Title).ToList();
}

<p>
    <a class="btn btn-info" href="/ControlPanel/ManageDishes/Add">Добавить блюдо</a>
    <a class="btn btn-success" href="javascript:showChangeDishTypeDialog();">Присвоить блюдам тип</a>
</p>
<table class="table table-bordered table-hover">
    <thead>
        <tr>
            <th></th>
            <th>ИД</th>
            <th>Наименование</th>
            <th>Ресторан</th>
            <th>Заказов</th>
            <th>Цена</th>
            <th style="width: 150px"></th>
        </tr>
    </thead>
    <tbody>
        @if (Model.Count == 0)
        {
            <tr>
                <td colspan="7">Не найдено блюд в системе</td>
            </tr>
        }
        else
        {
            foreach (var dish in Model)
            {
            <tr>
                <td>
                    <input type="checkbox" name="dish_@dish.Id" data-id="@dish.Id"/></td>
                <td>@dish.Id</td>
                <td>@dish.Title</td>
                <td>@dish.Cafe.Title</td>
                <td>@dish.OrderItems.Count</td>
                <td>@String.Format("{0:c}", dish.Price)</td>
                <td>
                    <a href="/ControlPanel/ManageDishes/Edit/@dish.Id">Редактировать</a>
                    <a href="/ControlPanel/ManageDishes/Delete/@dish.Id" onclick="return confirm('Действительно хотите удалить это блюдо? Блюдо нельзя удалить если оно было хоть раз заказано')">Удалить</a>
                </td>
            </tr>
            }
        }
    </tbody>
</table>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Изменение типа блюд</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" id="change-dish-type-form" method="POST" action="/ControlPanel/ManageDishes/ChangeTypes">
                    <input type="hidden" id="dish-ids" name="ids" value="" />
                    <div class="form-group">
                        <label>Новый тип блюда</label>
                        <select id="dish-type-field" class="form-control" name="typeId">
                            <option value="-1">Не указан</option>
                            @foreach (var group in dishTypes.GroupBy(g => g.GroupName))
                            {
                                <optgroup label="@group.Key">
                                    @foreach (var type in group)
                                    {
                                        <option value="@type.Id">@type.Title</option>
                                    }
                                </optgroup>
                            }
                        </select>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Отмена</button>
                <button type="button" class="btn btn-primary" onclick="$('#change-dish-type-form').submit();">Применить</button>
            </div>
        </div>
        <!-- /.modal-content -->
    </div>
    <!-- /.modal-dialog -->
</div>
<!-- /.modal -->
<script type="text/javascript">
    function showChangeDishTypeDialog() {
        var ids = "";
        $("td input:checked").each(function(index,item) {
            ids += $(item).data("id")+",";
        });
        $("#dish-ids").val(ids);
        $("#myModal").modal({
            keyboard: true
        });
    }
</script>